/**
 * Copyright By Grandsoft Company Limited.  
 * 2013-6-13 上午11:30:05
 */
package com.glodon.ggzy.user.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.glodon.ggzy.core.model.Page;
import com.glodon.ggzy.core.utils.JsonUtils;
import com.glodon.ggzy.core.utils.PageSearchModel;
import com.glodon.ggzy.core.utils.Tools;
import com.glodon.ggzy.entity.JgMenu;
import com.glodon.ggzy.entity.JgRole;
import com.glodon.ggzy.entity.JgUser;
import com.glodon.ggzy.user.service.IJgptUserService;
import com.glodon.ggzy.vo.JgptMenuVO;
import com.glodon.ggzy.vo.JgptRoleVO;
import com.glodon.ggzy.vo.JgptUserVO;

/**
 * 
 * 
 * 监管平台用户管理控制类
 * @author yangjf
 * @since jdk1.6
 * @date 2013-7-1
 *
 */
@Controller
public class JgptUserController {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private IJgptUserService jgptUserService;

	/**
	 * 
	 * 菜单集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/user/jgpt_menu_list")
	public String getMenuList(Model model) {
		return "/user/jgpt_menu_list";
	}

	/**
	 * 
	 * 菜单加载数据
	 * @param model
	 * @param rows
	 * @param page
	 * @param searchText
	 * @param response
	 */
	@RequestMapping("/user/jgpt_menu_data")
	public void getLoadMenuData(Model model, int rows, int page, String searchText, HttpServletResponse response) {
		PageSearchModel qm = new PageSearchModel();
		qm.setPageNo(page);
		qm.setPageSize(rows);
		if (!StringUtils.isEmpty(searchText)) {
			try {
				searchText = URLDecoder.decode(searchText, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		List<JgptMenuVO> jgptMenuList = new ArrayList<JgptMenuVO>();
		Page queryPage = jgptUserService.queryJgptMenu(qm, searchText);
		List<JgMenu> meunList = (List<JgMenu>) queryPage.getResult();
		for (JgMenu menu : meunList) {
			JgptMenuVO menuvo = new JgptMenuVO();
			menuvo.setId(menu.getId());
			menuvo.setMenuName(menu.getMenuName());
			menuvo.setMenuUrl(menu.getMenuUrl());
			menuvo.setOperation("<a href='jgpt_menu_edit.do?id=" + menu.getId() + "'>编辑</a>&nbsp;&nbsp;<a href='jgpt_menu_del.do?id=" + menu.getId()
					+ "'>删除</a>");
			jgptMenuList.add(menuvo);
		}
		queryPage.setResult(jgptMenuList);
		String jsonStr = "";
		try {
			jsonStr = JsonUtils.toJsonStr(queryPage);
			response.getWriter().write(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * 添加菜单跳转
	 * @param model
	 * @return
	 */
	@RequestMapping("/user/jgpt_menu_add")
	public String getGotoMenuAdd(Model model) {
		return "/user/jgpt_menu_add";
	}

	/**
	 * 
	 * 保存菜单
	 * @param meun
	 * @param model
	 * @param response
	 * @return
	 */
	@RequestMapping("/user/jgpt_menu_save")
	public ModelAndView addJgptMenu(JgMenu menu, Model model, HttpServletResponse response) {
		if (jgptUserService.saveJgptMenu(menu)) {
			return new ModelAndView("redirect:jgpt_menu_list.do");
		} else {
			Tools.repsonseJsAlert("添加菜单失败，请查看数据库连接是否正常！", response);
			return null;
		}
	}

	/**
	 * 
	 * 编辑菜单跳转
	 * @param model
	 * @return
	 */
	@RequestMapping("/user/jgpt_menu_edit")
	public String getGotoMenuEdit(Model model, String id) {
		JgMenu menu = jgptUserService.getJgMenuById(id);
		model.addAttribute("menu", menu);
		return "/user/jgpt_menu_edit";
	}

	/**
	 * 
	 * 更新菜单
	 * @param menu
	 * @param model
	 * @param response
	 * @return
	 */
	@RequestMapping("/user/jgpt_menu_update")
	public ModelAndView updateJgptMenu(JgMenu menu, Model model, HttpServletResponse response) {
		if (jgptUserService.saveJgptMenu(menu)) {
			return new ModelAndView("redirect:jgpt_menu_list.do");
		} else {
			Tools.repsonseJsAlert("添加菜单失败，请查看数据库连接是否正常！", response);
			return null;
		}
	}

	@RequestMapping("/user/jgpt_menu_del")
	public ModelAndView deleteMenu(Model model, String id) {
		jgptUserService.deletJgMenuById(id);
		return new ModelAndView("redirect:jgpt_menu_list.do");
	}

	/**
	 * 
	 * 获取角色集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/user/jgpt_role_list")
	public String getRoleList(Model model) {
		return "/user/jgpt_role_list";
	}

	/**
	 * 
	 * 角色数据加载
	 * @param model
	 * @param rows
	 * @param page
	 * @param searchText
	 * @param response
	 */
	@RequestMapping("/user/jgpt_role_data")
	public void getLoadRoleData(Model model, int rows, int page, String searchText, HttpServletResponse response) {
		PageSearchModel qm = new PageSearchModel();
		qm.setPageNo(page);
		qm.setPageSize(rows);
		if (!StringUtils.isEmpty(searchText)) {
			try {
				searchText = URLDecoder.decode(searchText, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		List<JgptRoleVO> jgptRoleList = new ArrayList<JgptRoleVO>();
		Page queryPage = jgptUserService.queryJgptRole(qm, searchText);
		List<JgRole> roleList = (List<JgRole>) queryPage.getResult();
		for (JgRole jgRole : roleList) {
			JgptRoleVO rolevo = new JgptRoleVO();
			rolevo.setId(jgRole.getId());
			rolevo.setRoleName(jgRole.getRoleName());
			rolevo.setRoleMeun(jgptUserService.getMeunNameByRoleId(jgRole.getId()));
			rolevo.setOperation("<a href='jgpt_role_edit.do?id=" + jgRole.getId() + "'>编辑</a>&nbsp;&nbsp;<a href='jgpt_role_del.do?id="
					+ jgRole.getId() + "'>删除</a>");
			jgptRoleList.add(rolevo);
		}
		queryPage.setResult(jgptRoleList);
		String jsonStr = "";
		try {
			jsonStr = JsonUtils.toJsonStr(queryPage);
			response.getWriter().write(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * 添加角色跳转
	 * @param model
	 * @return
	 */
	@RequestMapping("/user/jgpt_role_add")
	public String getGotoRoleAdd(Model model) {
		List<JgMenu> menuList = jgptUserService.getJgMenuAll();
		model.addAttribute("menuList", menuList);
		return "/user/jgpt_role_add";
	}

	/**
	 * 
	 * 保存角色
	 * @param meun
	 * @param model
	 * @param response
	 * @return
	 */
	@RequestMapping("/user/jgpt_role_save")
	public ModelAndView addRoleMenu(JgRole role, String menuIds, Model model, HttpServletResponse response) {
		if (jgptUserService.saveJgptRole(role, menuIds)) {
			return new ModelAndView("redirect:jgpt_role_list.do");
		} else {
			Tools.repsonseJsAlert("添加角色失败，请查看数据库连接是否正常！", response);
			return null;
		}
	}

	/**
	 * 
	 * 编辑角色跳转
	 * @param model
	 * @return
	 */
	@RequestMapping("/user/jgpt_role_edit")
	public String getGotoRoleEdit(Model model, String id) {
		JgRole role = jgptUserService.getJgRoleById(id);
		List<JgptMenuVO> jgptMenuList = new ArrayList<JgptMenuVO>();
		List<JgMenu> menuList = jgptUserService.getJgMenuAll();
		for (JgMenu menu : menuList) {
			JgptMenuVO jgptMenu = new JgptMenuVO();
			jgptMenu.setId(menu.getId());
			jgptMenu.setMenuName(menu.getMenuName());
			jgptMenu.setCheckedId(jgptUserService.getCheckedMenuId(id, menu.getId()));
			jgptMenuList.add(jgptMenu);
		}
		model.addAttribute("role", role);
		model.addAttribute("menuList", jgptMenuList);
		return "/user/jgpt_role_edit";
	}

	/**
	 * 
	 * 更新角色
	 * @param role
	 * @param menuIds
	 * @param model
	 * @param response
	 * @return
	 */
	@RequestMapping("/user/jgpt_role_update")
	public ModelAndView addRoleEdit(JgRole role, String menuIds, Model model, HttpServletResponse response) {
		if (jgptUserService.saveJgptRole(role, menuIds)) {
			return new ModelAndView("redirect:jgpt_role_list.do");
		} else {
			Tools.repsonseJsAlert("添加角色失败，请查看数据库连接是否正常！", response);
			return null;
		}
	}

	/**
	 * 
	 * 删除角色
	 * @param model
	 * @return
	 */
	@RequestMapping("/user/jgpt_role_del")
	public ModelAndView deleteRole(Model model, String id) {
		jgptUserService.deletJgRoleById(id);
		return new ModelAndView("redirect:jgpt_role_list.do");
	}

	/**
	 * 
	 * 获取用户集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/user/jgpt_user_list")
	public String getUserList(Model model) {
		return "/user/jgpt_user_list";
	}

	@RequestMapping("/user/jgpt_user_data")
	public void getLoadUserData(Model model, int rows, int page, String searchText, HttpServletResponse response) {
		PageSearchModel qm = new PageSearchModel();
		qm.setPageNo(page);
		qm.setPageSize(rows);
		if (!StringUtils.isEmpty(searchText)) {
			try {
				searchText = URLDecoder.decode(searchText, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		List<JgptUserVO> jgptUserList = new ArrayList<JgptUserVO>();
		Page queryPage = jgptUserService.queryJgptUser(qm, searchText);
		List<JgUser> userList = (List<JgUser>) queryPage.getResult();
		for (JgUser user : userList) {
			JgptUserVO uservo = new JgptUserVO();
			uservo.setId(user.getId());
			uservo.setUserName(user.getUserName());
			uservo.setPassword(user.getPassword());
			uservo.setRoleName(jgptUserService.getJgRoleById(user.getRoleId()).getRoleName());
			uservo.setOperation("<a href='jgpt_user_edit.do?id=" + user.getId() + "'>编辑</a>&nbsp;&nbsp;<a href='jgpt_user_del.do?id=" + user.getId()
					+ "'>删除</a>");
			jgptUserList.add(uservo);
		}
		queryPage.setResult(jgptUserList);
		String jsonStr = "";
		try {
			jsonStr = JsonUtils.toJsonStr(queryPage);
			response.getWriter().write(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * 添加用户跳转
	 * @param model
	 * @return
	 */
	@RequestMapping("/user/jgpt_user_add")
	public String getGotoUserAdd(Model model) {
		List<JgRole> roleList = jgptUserService.getJgRoleAll();
		model.addAttribute("roleList", roleList);
		return "/user/jgpt_user_add";
	}

	/**
	 * 
	 * 保存用户
	 * @param meun
	 * @param model
	 * @param response
	 * @return
	 */
	@RequestMapping("/user/jgpt_user_save")
	public ModelAndView addUserMenu(JgUser user, Model model, HttpServletResponse response) {
		JgUser isExistUser = jgptUserService.isUserNameExist(user.getUserName());
		if (isExistUser != null) {
			String errStr = "数据库中已存在此用户名！";
			logger.error(errStr);
			Tools.repsonseJsAlert(errStr, response);
			return null;
		}
		if (jgptUserService.saveJgptUser(user)) {
			return new ModelAndView("redirect:jgpt_user_list.do");
		} else {
			Tools.repsonseJsAlert("添加用户失败，请查看数据库连接是否正常！", response);
			return null;
		}
	}

	/**
	 * 
	 * 添加用户跳转
	 * @param model
	 * @return
	 */
	@RequestMapping("/user/jgpt_user_edit")
	public String getGotoUserEdit(Model model, String id) {
		JgUser user = jgptUserService.getJgUserById(id);
		List<JgRole> roleList = jgptUserService.getJgRoleAll();
		model.addAttribute("user", user);
		model.addAttribute("roleList", roleList);
		return "/user/jgpt_user_edit";
	}

	@RequestMapping("/user/jgpt_user_update")
	public ModelAndView addUserEdit(JgUser user, Model model, HttpServletResponse response) {
		if (jgptUserService.saveJgptUser(user)) {
			return new ModelAndView("redirect:jgpt_user_list.do");
		} else {
			Tools.repsonseJsAlert("添加用户失败，请查看数据库连接是否正常！", response);
			return null;
		}
	}

	/**
	 * 
	 * 删除用户
	 * @param model
	 * @return
	 */
	@RequestMapping("/user/jgpt_user_del")
	public ModelAndView deleteUser(Model model, String id) {
		jgptUserService.deletJgUserById(id);
		return new ModelAndView("redirect:jgpt_user_list.do");
	}

}
